package io.zhifei.modules.sys.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.zhifei.common.annotation.SysLog;
import io.zhifei.common.utils.ApiResponse;
import io.zhifei.common.utils.Constant;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.R;
import io.zhifei.modules.sys.dataStatistical.entity.FollowerChooseInfo;
import io.zhifei.modules.sys.dataStatistical.util.UserFunction;
import io.zhifei.modules.sys.system.entity.SysOrgEntity;
import io.zhifei.modules.sys.system.entity.SysUserEntity;
import io.zhifei.modules.sys.system.service.SysOrgService;
import io.zhifei.modules.sys.system.service.SysUserRoleService;
import io.zhifei.modules.sys.system.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p> 后台部门管理 </p>
 *
 * @author： WangPeng <br/>
 * @date： 2020/11/10 15:26 <br/>
 * @version： <br/>
 */
@RestController
@RequestMapping("/sys/org")
@Api(description = ("后台部门管理"))
public class SysOrgController extends AbstractController {

    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 所有部门列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("sys:org:list")
    @ApiOperation("部门列表")
    public R list(@RequestParam Map<String, Object> params) {

        //只有超级管理员，才能查看所有管理员列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            params.put("orgId", null);
        } else {
            params.put("orgId", params.get("orgId"));
        }

        PageUtils page = sysOrgService.getOrgList(params);
        return R.ok().put("page", page);
    }

    @SysLog("新增部门")
    @PostMapping("/add")
//    @RequiresPermissions("sys:org:add")
    @ApiOperation("新增部门")
    public R add(@RequestBody SysOrgEntity sysOrgEntity) {

        SysUserEntity sysUserEntity = getUser();
        FollowerChooseInfo followerChooseInfo = UserFunction.dataStatisticalByFollower(sysUserEntity);
        //当只是业务员时查看自己的数据
        if (followerChooseInfo.getMyself() > 0
                && followerChooseInfo.getCampus() == 0
                && followerChooseInfo.getAllCampus() == 0
                && followerChooseInfo.getManagement() == 0){
            return R.ok().put("500", "没有权限，请联系管理员授权");
        }
        try {
            sysOrgService.addOrg(sysOrgEntity);
            return R.ok().put("200", "新增成功！");
        } catch (Exception e) {
            return R.ok().put("500", e.getMessage());
        }
    }

    @PostMapping("/update")
//    @RequiresPermissions("sys:org:update")
    @ApiOperation("编辑部门")
    public ApiResponse<Object> update(@RequestBody SysOrgEntity sysOrgEntity) {
        //除业务员外的都可以修改部门
        SysUserEntity sysUserEntity = getUser();
        FollowerChooseInfo followerChooseInfo = UserFunction.dataStatisticalByFollower(sysUserEntity);
        //当只是业务员时查看自己的数据
        if (followerChooseInfo.getMyself() > 0
                && followerChooseInfo.getCampus() == 0
                && followerChooseInfo.getAllCampus() == 0
                && followerChooseInfo.getManagement() == 0){
            return new ApiResponse<>("500", "没有权限，请联系管理员授权");
        }
        try {
            sysOrgService.updateOrg(sysOrgEntity);
            return new ApiResponse<>("200", "修改成功！");
        } catch (Exception e) {
            return new ApiResponse<>("500", e.getMessage());
        }
    }

    @PostMapping("/delete")
//    @RequiresPermissions("sys:org:delete")
    @ApiOperation("删除部门")
    public R delete(@RequestBody Long[] ids) {
        List<Long> orgIds = new ArrayList<>();
        //除业务员外的都可以删除部门
        SysUserEntity sysUserEntity = getUser();
        FollowerChooseInfo followerChooseInfo = UserFunction.dataStatisticalByFollower(sysUserEntity);
        if (followerChooseInfo.getMyself() > 0
                && followerChooseInfo.getCampus() == 0
                && followerChooseInfo.getAllCampus() == 0
                && followerChooseInfo.getManagement() == 0){
            return R.ok().put("500", "没有权限，请联系管理员授权");
        }
        try {
            orgIds = Arrays.asList(ids);
            sysOrgService.deleteOrg(orgIds);
            return R.ok().put("200", "删除成功！");
        } catch (Exception e) {
            return R.ok().put("500", e.getMessage());
        }
    }

    @GetMapping("/orgTree")
    @ApiOperation("部门树")
    public R orgTree() {
        try {
            return R.ok().put("orgTree", sysOrgService.orgTree());
        } catch (Exception e) {
            return R.ok().put("500", e.getMessage());
        }
    }

    @GetMapping("/orgList")
    @ApiOperation("部门列表")
    public R orgList() {
        try {
            return R.ok().put("orgList", sysOrgService.orgList());
        } catch (Exception e) {
            return R.ok().put("500", e.getMessage());
        }
    }
}
